#include "StdAfx.h"
#include "ClientHandler.h"
#include "Libs/Native/Common/FastLogger.h"
#include "Libs/Native/Messages/Message.P2.QuotesDump.h"

#include <sstream>

ClientHandler::ClientHandler(void)
{
}

ClientHandler::~ClientHandler(void)
{
}

void ClientHandler::OnRequest(const AlgoTrading::Messages::RequestWithResponse& request)
{
	std::wostringstream os;
	os << L"RequestWithResponse recieved: requestId " << request.GetRequestId() << " requestType " << static_cast<int>(request.GetRequestWithResponseMessageType());
	std::wstring* logEntry = new std::wstring(os.str());
	AlgoTrading::Logging::fastLogger.Log((void*) logEntry, AlgoTrading::Logging::FastLogger::WIDE_STRING);

	AlgoTrading::Messages::P2QuotesDump dump;
	dump.SetRequestId(request.GetRequestId());
	dump.PushQuote(9);

	SendRequestResponse(dump);
}

void ClientHandler::OnRequest(const AlgoTrading::Messages::RequestWithoutResponse& request)
{
	std::wostringstream os;
	os << L"RequestWithoutResponse recieved:  requestType " << static_cast<int>(request.GetRequestWithoutResponseMessageType());
	std::wstring* logEntry = new std::wstring(os.str());
	AlgoTrading::Logging::fastLogger.Log((void*) logEntry, AlgoTrading::Logging::FastLogger::WIDE_STRING);
}

std::tr1::shared_ptr<ClientRequestProcessor> ClientHandler::GetGlientRequestProcessor()
{
	return shared_from_this();
}

ClientRequestProcessorPtr ClientConnectionAcceptorImpl::OnClientConnected(std::tr1::weak_ptr<ClientNotificationProcessor> client)
{
	std::tr1::shared_ptr<ClientHandler> clientHandler(new ClientHandler());
	return clientHandler->OnClientConnected(client);
}

void ClientConnectionAcceptorImpl::OnError(const boost::system::error_code& error)
{
	std::ostringstream os;
	os << "Error occured: " << error.message();
	std::string* logEntry = new std::string(os.str());
	AlgoTrading::Logging::fastLogger.Log((void*) logEntry, AlgoTrading::Logging::FastLogger::STRING);
}

void ClientConnectionAcceptorImpl::OnDisconnect()
{
	std::ostringstream os;
	os << "Client disconnected";
	std::string* logEntry = new std::string(os.str());
	AlgoTrading::Logging::fastLogger.Log((void*) logEntry, AlgoTrading::Logging::FastLogger::STRING);
}

ClientConnectionAcceptorImpl::~ClientConnectionAcceptorImpl()
{
}